﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using WMSADL.SY;
using WMSAML.SY;

namespace WMSABL.SY
{
    public class TUserGroupRelation_BLL
    {
        TUserGroupRelation_DAL dal = new TUserGroupRelation_DAL();

        /// <summary>
        /// 
        /// </summary>
        public int Add(TUserGroupRelation_Entity entity)
        {
            return dal.Add(entity);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        public bool UpdateTUserGroupRelation(int uid, string gidstr)
        {
            int result = 0;
            if (dal.ExistsByUID(uid))
            {
                Delete(uid);
            }
            if (gidstr == "")
            {
                return true;
            }
            string[] arr = gidstr.Split(',');
            TUserGroupRelation_Entity enity = new TUserGroupRelation_Entity();
            enity.User_id = uid;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                enity.Group_id = Convert.ToInt32(arr[i]);
                if (Add(enity) > 0) result++;
            }
            if (result > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// BYUserID BY UserID删除用户所有角色
        /// </summary>
        public bool Delete(int id)
        {
            return dal.Delete(id);
        }

        /// <summary>
        /// BY Uidand Gid
        /// </summary>
        public bool Delete(int uid, int gid)
        {
            return dal.Delete(uid, gid);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(TUserGroupRelation_Entity entity)
        {
            return dal.Update(entity);
        }

        /// <summary>
        /// ByID
        /// </summary>
        public bool ExistsByID(int id)
        {
            return dal.ExistsByID(id);
        }

        /// <summary>
        /// ByID
        /// </summary>
        /// <summary>
        public TUserGroupRelation_Entity GetEntityByID(int id)
        {
            return dal.GetEntityByID(id);
        }
    }
}
